# 版本说明:  2024.12.09 新建
# 制作人员:  dzy
# 功能说明： 专用用于各种api业务层测试
# 备注说明：
# 环境要求： python3/ windows32or64
# 设备要求：


# ---内置库---
from datetime import datetime, timedelta
from datetime import datetime
import os
import sys
import time

# ---第三方库---
from colorama import init, Fore, Back, Style

# ---自研库---
from lib.mytools import getNowTime
from lib.mytools import basicClass
from core.Operator_DB import Sqlite_Operator


def create_level_table():  # TODO
    # 用于自动建表,尚未开发
    pass


def make_data_to_db():  # 用于写入到数据库中
    # 这个函数只是往数据库中写入数据而已
    tester_db_opt = Sqlite_Operator()
    tester_db_opt.connect('data\data.db')

    # 变化因子, 以下的参数是根据业务需要进行调整的
    month = 5  # 月份
    searchFlag = 'G1_lk_' + str(month)
    supplier = 'LK_20241203'
    supplierLot_part2 = '_01'  # 模具号

    materialNumber = '311201020016'  # 根据业务需求改
    materialName = '46275双通铝壳'  # 根据业务需求改

    supplierLot_part1 = 120301  # 供应商批次前缀
    inspectionLot_part1 = 'jyd'+str(supplierLot_part1)
    inspectionLot_part2 = 1

    wanted_dict = {
        "iid": 10101,
        "endDate": "endDate",
        "inspectionDate": "inspectionDate",
        "startDate": "startDate",
        "inspectionLot": "inspectionLot",
        "materialName": "materialName",
        "materialNumber": "materialNumber",
        "purchaseOrder": "purchaseOrder",
        "supplier": "supplier",
        "supplierLot": "supplierLot",
    }
    # 开始造数据...
    flow_id = 0
    for daynum in range(1, 27):  # 一个月27天
        specific_date = datetime(2024, month, daynum)

        for hour in range(9, 20):  # 一天12小时, 一小时一个批次
            flow_id += 1
            start_date = specific_date + timedelta(days=0)
            start_date_str = start_date.strftime('%Y-%m-%d 00:00:00')

            inspection_Date = start_date
            inspection_Date_str = inspection_Date.strftime(
                f'%Y-%m-%d {hour}:30:22')

            end_date = specific_date + timedelta(days=1)
            end_date_str = end_date.strftime('%Y-%m-%d 00:00:00')

            wanted_dict['iid'] = int(
                str(supplierLot_part1)+str(month)+str(daynum)+str(hour)+str(flow_id))
            wanted_dict['startDate'] = start_date_str
            wanted_dict['inspectionDate'] = inspection_Date_str
            wanted_dict['endDate'] = end_date_str
            wanted_dict['materialNumber'] = materialNumber
            wanted_dict['materialName'] = materialName
            wanted_dict['supplierLot'] = str(
                supplierLot_part1)+str(month)+str(daynum)+str(hour) + supplierLot_part2

            wanted_dict['supplier'] = supplier
            # inspection_Date.date()
            # 检验批用 头 月 日 小时
            wanted_dict['inspectionLot'] = inspectionLot_part1 + \
                str(inspection_Date.month)+str(inspection_Date.day) + str(hour)
            print(wanted_dict)
            # 写入到数据库中,

            iid = wanted_dict['iid']
            checkNumber = 1000
            decisionResults = '合格'
            detailReqList = 0
            endDate = wanted_dict['endDate']
            factory = 1000
            finalDecisionId = 1
            inspectionDamage = 0
            inspectionDate = wanted_dict['inspectionDate']
            inspectionLot = wanted_dict['inspectionLot']
            materialName = wanted_dict['materialName']
            materialNumber = wanted_dict['materialNumber']
            purchaseOrder = 0
            purchaseOrderId = 0
            returnNumber = 0
            sourceDocuments = 'SAP'
            startDate = wanted_dict['startDate']
            supplier = wanted_dict['supplier']
            supplierLot = wanted_dict['supplierLot']
            testType = 1
            thoseResponsible = searchFlag
            sqlstring1 = 'INSERT INTO inspection_bill (iid,checkNumber,decisionResults,detailReqList,endDate,factory,finalDecisionId,inspectionDamage,inspectionDate,inspectionLot,materialName,materialNumber,purchaseOrder,purchaseOrderId,returnNumber,sourceDocuments,startDate,supplier,supplierLot,testType,thoseResponsible) '
            sqlstring2 = f"VALUES ({iid},'{checkNumber}','{decisionResults}','{detailReqList}','{endDate}','{factory}','{finalDecisionId}','{inspectionDamage}','{inspectionDate}','{inspectionLot}','{materialName}','{materialNumber}','{purchaseOrder}','{purchaseOrderId}','{returnNumber}','{sourceDocuments}','{startDate}','{supplier}','{supplierLot}','{testType}','{thoseResponsible}');"
            print(sqlstring1+sqlstring2)
            res = tester_db_opt.execute_sql(sqlstring1+sqlstring2)
            time.sleep(0.01)


if __name__ == '__main__':
    make_data_to_db()
    pass
